package org.pet.mapper;

import org.apache.ibatis.annotations.*;
import org.pet.entity.User;
import org.pet.entity.UserVo;

@Mapper
public interface UserMapper {
    //微信用户
    @Select("select id,name,nick_name,password,openid,phone,email,birthday,pic,gender,create_time,modify_time,delete_time,remove " +
            "from user where openid=#{openid};")
    User getUserByOpenid(String openid);

    @Insert("insert into user (name,nick_name,password,openid,pic,create_time,modify_time,remove) " +
            "values (#{name},#{nickName},#{password},#{openid},#{pic},#{createTime},#{modifyTime},#{remove})")
    int wxInsert(User user);

    //PC用户
    @Select("select id,name,nick_name,password,openid,phone,email,birthday,pic,gender,create_time,modify_time,delete_time,remove " +
            "from user where name=#{name};")
    User getUserByUsername(String name);

    @Select("select id,name,nick_name,password,openid,phone,email,birthday,pic,gender,create_time,modify_time,delete_time,remove " +
            "from user where id=#{id};")
    UserVo getUserById(Integer id);

    @Select("select id,name,nick_name,password,openid,phone,email,birthday,pic,gender,create_time,modify_time,delete_time,remove " +
            "from user where id=#{id};")
    User getUser(Integer id);

    //插入
    @Insert("insert into user (name,password,phone,create_time,modify_time,remove) " +
            "values (#{name},#{password},#{phone},#{createTime},#{modifyTime},#{remove})")
    int insert(User user);

    //更新
    @Update({
            "<script>",
            "update user",
            "<set>",
            "<if test = 'birthday != null'>birthday=#{birthday},</if>",
            "<if test = 'nickName != null'>nick_name=#{nickName},</if>",
            "<if test = 'email != null'>email=#{email},</if>",
            "<if test = 'gender != null'>gender=#{gender},</if>",
            "<if test = 'pic != null'>pic=#{pic},</if>",
            "modify_time=now()",
            "</set>",
            "where id=#{id}",
            "</script>"})
    int update(User user);

    @Update("update user set password=#{pwd},modify_time=now() where id=#{id}")
    int updatePwd(Integer id,String pwd);

    //删除
    @Update("update user set delete_time=now(),remove=1 where id=#{id}")
    int delete(Integer id);
}
